$(function(){
	////皮肤
	$('#colorpifu').on("input",function(){
		var changecolor=$('#colorpifu').val();
		$('nav').css("background",changecolor);
		$('.footer').css("background",changecolor);
	})
	
	//添加播放列表
for (var i=0;i<musicData.length;i++) {
	 var item = musicData[i];
	 var li = $("<li>"+item.song+"</li>");
	 $('#gequ').append(li);
}
//添加历史记录，播放过的进入历史记录，历史记录不重复
        $('#gequ li').click(function(){
            console.log($(this).text());
          if ($('#historylist').text().indexOf($(this).text())===-1) {
                $('#historylist').append($(this).clone());
            }
          
          var aas=$(this).index()+1;///记录下标
          
          $('#historylist li').click(function(){
          		myAudio.src=musicData[aas].url;
          		myAudio.play();
          		
          })
          
	})
	/*// 增加功能：单击历史记录，也能正常播放歌曲*/
	$('.history').click(function(){
		$('.boList').toggle();
	})
	
	$("#gequ li").click(ggeci)
	function ggeci(){
		 i=$(this).index();
		var item=musicData[i];
	////////给音频标签src值      //展示歌曲相关信息
		$('audio').attr('src',item.url);
		$('.lis1').text(item.song);
		$('.lis3 .geshou').text(item.singer);
		$('.zhuanji').text(item.album);
		$('.zhuanji2').text(item.source);
		$('.content img').attr('src',item.cover);
		
		
		
		$("#geci #lyric").empty();
		for(var i=0;i<lrcs.length;i++){
			var lrcsObj=lrcs[i];
			if(lrcsObj.song==item.lyric){
				var lrcArr=lrcsObj.lyric;
			for(var k=0;k<lrcArr.length;k++){
				var lrcInf=lrcArr[k];
				var li=$(`<li time=${lrcInf.time}>${lrcInf.lineLyric}</li>`);
				$("#geci #lyric").append(li);
			}
			break;
			}
		}
		
	}
	
	////////////////////////歌词随着音乐的播放同步滚动
	$('audio').on('timeupdate',function(){
		var listes=$('#lyric li');
		var times=Math.ceil(this.currentTime);
		for(var i=0;i<listes.length;i++){
			if($(listes[i]).attr('time')==times){
				listes.removeClass('red');
				$(listes[i]).addClass('red');
				
				document.getElementById("lyric").scrollTop = i*13;
                break;
			}
		}
		
	})

	
	var myAudio=$('audio').get(0);
	var musicIndex=0;
	function changemusic(idx){
		myAudio.src=musicData[idx].url;
		$('.lis1').text(musicData[idx].song);
		$('.lis3 .geshou').text(musicData[idx].singer);
		$('.zhuanji').text(musicData[idx].album);
		$('.zhuanji2').text(musicData[idx].source);
		$('.content img').attr('src',musicData[idx].cover);
		
		$('#charu1').empty();
		$('#charu1').append(musicData[idx].msg[0].megCon);
		$('.listzi1').empty();
		$('.listzi1').append(musicData[idx].msg[0].date);
		$('#charu2').empty();
		$('#charu2').append(musicData[idx].msg[1].megCon);
		$('.listzi3').empty();
		$('.listzi3').append(musicData[idx].msg[1].date);
		$('#tu1 img').empty();
		$('#tu1 img').attr("src",musicData[idx].cover);
	
		var item=musicData[musicIndex];
		$("#geci #lyric").empty();
		for(var i=0;i<lrcs.length;i++){
			var lrcsObj=lrcs[i];
			if(lrcsObj.song==item.lyric){
				var lrcArr=lrcsObj.lyric;
			for(var k=0;k<lrcArr.length;k++){
				var lrcInf=lrcArr[k];
				var li=$(`<li time=${lrcInf.time}>${lrcInf.lineLyric}</li>`);
				$("#geci #lyric").append(li);
			}
			break;
			}
		}
	}
	changemusic(musicIndex);
	
	
	
//加载完成
	$('audio').on('canplaythrough',function(){
		var totalTime=myAudio.duration;
		$('#playProgress').attr('max',totalTime);
		$('#endTime').text(formatSeconds(totalTime));
	})
	////音频时长处理
	function formatSeconds(s) {
		var ss = parseInt(s);
		var hour = parseInt(ss / 3600);
		var minute = parseInt(ss % 3600 / 60);
		ss = ss % 60;
		return `${hour>0?(hour+":"):""}${minute}:${ss>9?ss:('0'+ss)}`;
	}
////////播放暂停
	$('#bo').click(function(){
		if(myAudio.paused){//播放
			myAudio.play();
			bozan();
			$('.left span:nth-child(2)').attr("id","zan");
		}else{
			myAudio.pause();//暂停
			bozan();
			$('.left span:nth-child(2)').attr("id","bo");
			
		}
	})
	
	//对歌曲播放暂停判断进行封装
	var alpa=0;
	var isplay=false;
	var timer;
	function bozan(){
			clearInterval(timer);
			isplay=!isplay;
			console.log(isplay);
			if(isplay){
				timer=setInterval(function(){
				alpa++;
				$('.content').css({
					transform:'rotate('+alpa+'deg)'
				})
				},20)
			
		}else{
			clearInterval(timer);
		
		}
	}

///////音频播放条 进度条时间
		
		myAudio.ontimeupdate=function(){
			$('#playProgress').val(this.currentTime);//this.currentTime 当前进度条时间
			$("#startTime").text(formatSeconds(this.currentTime));
		}

//////////////上一首|||下一首
		$('#lastSong').click(function(){
			musicIndex--;
			isplay=false;
			if (musicIndex== -1) {
			musicIndex=musicData.length-1;
			}
			changemusic(musicIndex);
			clearInterval(timer);
			bozan();
			$('.left span:nth-child(2)').attr("id","");
			myAudio.play();
			if(myAudio.paused){
				$('.left span:nth-child(2)').attr("id","bo");
			}else{
				$('.left span:nth-child(2)').attr("id","zan");
			}
			
			
		})
/////////////下一首
$('#nextSong').click(playnext)
function playnext(){
	musicIndex++;
    isplay=false;
	if(musicIndex==musicData.length){
		musicIndex=0;
	}
	changemusic(musicIndex);
	clearInterval(timer);
	bozan();
	myAudio.play();
	if(myAudio.paused){
				$('.left span:nth-child(2)').attr("id","bo");
			}else{
				$('.left span:nth-child(2)').attr("id","zan");
			}
	
}

/////////////拖动播放进度条改变播放进度

var isPlay;
$('#playProgress').change(function(){
	var presntime=$(this).val();
	console.log(presntime);
	myAudio.currentTime=presntime;
	$("#startTime").text(formatSeconds(presntime));
	
	if(isPlay){
		myAudio.play();
	}	
})
$("#playProgress").on("input",function(){
	var presntime=$(this).val();
	$('#startTime').text(formatSeconds(presntime));
})

$('#playProgress').mousedown(function(){
	if(myAudio.paused){
		isPlay=false;
		$('.left span:nth-child(2)').attr("id","bo");
	}else{
		isPlay=false;
		myAudio.pause();
		$('.left span:nth-child(2)').attr("id","zan");
	}
})
/////////静音
$(".footer #Ring").click(function(){
	if(myAudio.volume!=0){//有声音
		myAudio.volume=0;
		$('#yinProgress').val(0);
		$(".voice span").attr("id","jing");
	}else{
		myAudio.volume=0.4;
		$('#yinProgress').val(0.4);
		$(".voice span").attr("id","Ring");
	}
})

$('#yinProgress').on("input",function(){
	myAudio.volume=$(this).val();//当前音量=inputvalue
	if(myAudio.volume==0){//没有声音
		$(".voice span").attr("id","jing");
	}else{
		$(".voice span").attr("id","Ring");
	}
})
//////////////模式
var moshi=['顺序模式','循环模式','单曲循环'];
var playIdex=0;
$('.circulate').click(function(){
	playIdex++;
	$(this).attr('title',moshi[playIdex]);
	if(playIdex==1){
			$('.xunmo div').attr("class","circulate");
		}
		if(playIdex==2){
			$('.xunmo div').attr("class","xunhuan");
		}
		if(playIdex>2){
			$('.xunmo div').attr("class","danqu");
			playIdex=0;
		}
})
/////////////确定一首歌曲播放完成
myAudio.onended=function(){
		switch(playIdex){
			case 0:
			  musicIndex++;
			  if(musicIndex<musicData.length){
			  	changemusic(musicIndex);
			  	myAudio.play();
			  }else{
			  	musicIndex = musicData.length-1;
			  }
			  break;
			case 1:
			  	playnext();
			  	break;
			case 2:
			  	myAudio.play();
			  	break;
		}
	}


	
})
